﻿var url = getUrlId().split('=');
var id = url[0];
var date = url[1].split('#')[0].split('-');
var y = parseInt(date[0], 10);
var m = parseInt(date[1], 10);
// console.log(y, m)

// document.getElementById('nav').innerHTML = template('navs', navinfo5);

$('#nav li').eq(1).addClass('active');

load();
function load() {
  $.ajax({
    url: '/api/CommonApi?API=GetTeacherMonthApplyDetails',
    type: 'get',
    data: {
      TeacherMonthApplyID: url[0]
    },
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        // console.log(data)
        // console.log(JSON.stringify(data))
        var datas = {};
        datas.data = data.ApiParamObj;
        // 开始时间
        datas.s = new Date(datas.data[0].ApplyDate).pattern('yyyy-MM-dd');
        // 结束时间
        datas.e = new Date(datas.data[datas.data.length - 1].ApplyDate).pattern('yyyy-MM-dd');
        // console.log(datas.s, datas.e)
        // 差旅天数
        // datas.sDays = getDaysBetween(datas.data[datas.data.length - 1].ApplyDate, datas.data[0].ApplyDate);
        datas.sDays = getDaysBetween(datas.s, datas.e);
        datas.sDays = datas.sDays === 0 ? 1 : datas.sDays;
        // 执行天数
        // datas.eDays = getDaysBetween(datas.data[datas.data.length - 1].ApplyDate, datas.data[0].ApplyDate);
        datas.eDays = getDaysBetween(datas.s, datas.e);
        datas.eDays = datas.eDays === 0 ? 1 : datas.eDays;
        var arr = {};
        var o = datas.data;
        var i = 0;
        var l = o.length;
        var type = '';
        for (; i < l; i++) {
          type = o[i].CostType.split('-')[0];
          // console.log(o[i])
          if (!arr[type]) {
            arr[type] = [];
          }
          arr[type].push(o[i].CostMoney);
        }
        // 生成费用名称对应的a-z的字母
        var keys = {};
        i = 0;
        var cost = 0;
        datas.allCost = 0;
        for (var key in arr) {
          cost = (function () {
            var i = 0;
            var l = arr[key].length;
            var c = 0;
            // console.log(arr[key])
            for (;i < l;i++) {
              c += arr[key][i];
            }
            return c;
          }());
          // console.log(cost)
          keys[getAToZ()[i]] = {
            name: getAToZ()[i],
            value: key,
            cost: cost
          };
          i++;

          datas.allCost += cost;
        }
        i = 0;
        datas.keys = keys;
        // console.log(datas);

        document.getElementById('print').innerHTML = template('printTable', datas)
      }
    }
  });
}

function getDaysBetween(s, e) {
  var start = new Date(s).getTime();
  var end = new Date(e).getTime();
  return (Math.floor((end - start) / (24 * 3600 * 1000)));
}

$('#print').on('click', '#pass', function () {
  var submitObj = {
    Role:'FINANCE',
    TeacherMonthApplyID: id,
    State:'6_FINANCE_PASS',
    Explain:''
  };
  $.ajax({
    url: '/api/CommonApi?API=TeacherMonthApplyAuditByRole',
    type: 'get',
    data: submitObj,
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log(data);
        load();
        // showPage(lsInfo);
      }
    }
  });
});

$('#returnReject').click(function () {
  $('#rejectReason').modal('hide');
  $('#detailList').modal('show');
});

$('#print').on('click', '#reject', function () {
  $('#rejectReason').modal('show');
  /*var submitObj = {
    Role:'FINANCE',
    TeacherMonthApplyID: id,
    State:'-1_REJECT',
    Explain:''
  };
  $.ajax({
    url: '/api/CommonApi?API=TeacherMonthApplyAuditByRole',
    type: 'get',
    data: submitObj,
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log(data);
        load();
        // showPage(lsInfo);
      }
    }
  });*/
});

$('#rejectConfirm').click(function () {
  var v = $('#rejectDetail').val();
  if (!v) {
    alert('请输入驳回理由！');
    return false;
  }
  loading.show();
  var submitObj = {
    Role:'FINANCE',
    TeacherMonthApplyID: id,
    State:'-1_REJECT',
    Explain: v
  };
  $.ajax({
    url: '/api/CommonApi?API=TeacherMonthApplyAuditByRole',
    type: 'get',
    data: submitObj,
    dataType: 'json',
    success: function (data) {
      if (data.Success) {
        console.log(data);
        $('#rejectReason').modal('hide');
        loading.hide();
        load();
        // showPage(lsInfo);
      }
    }
  });
});